<?php

/**
 * 公共方法
 * File Name：PublicController.php
 * File Encoding：UTF-8
 * File New Time：2014-4-29 15:50:31
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class PublicController extends AdminBase {

    //特殊的控制器方法
    public function actions() {
        return array(
            //验证码
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'transparent' => true, //是否使用透明背景
                'maxLength' => 6, // 最多生成几个字符
                'minLength' => 4, // 最少生成几个字符
            ),
        );
    }

    //显示后台登录界面
    public function actionLogin() {
        if (IS_POST) {
            $username = Yii::app()->request->getParam('username');
            $password = Yii::app()->request->getParam('password');
            if (empty($username)) {
                $this->error('用户名不能为空！');
            }
            if (empty($password)) {
                $this->error('请输入密码！');
            }
            if (!$this->validateVerifyCode(Yii::app()->request->getParam('code'))) {
                $this->error('验证码错误，请重新输入！');
            }
            //验证登录
            if (Yii::app()->passport->loginAdmin($username, $password)) {
                //$this->success('登录成功！', self::U('index/index'));
                $this->redirect(array('index/index'));
            } else {
                $error = Yii::app()->passport->error ? Yii::app()->passport->error : '登录失败！';
                $this->error($error);
            }
        } else {
            //如果已经登录，直接跳转到后台首页
            if (AdminBase::$uid) {
                $this->redirect(array('index/index'));
            }
            $this->render('login');
        }
    }

    //后台显示指定tag列表，用于编辑添加时显示
    public function actionTags() {
        if (empty(self::$uid)) {
            $this->redirect(array('public/login'));
            return false;
        }
        $tag = Yii::app()->request->getParam('tag');
        if (empty($tag)) {
            $this->error('标签不能为空！');
        }
        $model = TagsContentModel::model();
        $time = time();
        $where = array(
            'tag' => $tag,
//            't.start_time' => array('LT', $time),
//            't.end_time' => array('EGT', $time),
            'order' => 'GoodsModel.view desc,updatetime desc',
        );
        //分页
        $pageId = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        //分页数
        $limit = 20;
        $criteria = $model->where($where);
        //信息总数
        $count = $model->count($criteria);
        //分页
        $page = self::page($count, $limit, $pageId);
        //查询信息
        $criteria->limit = $page->listRows;
        $criteria->offset = $page->firstRow;
        $dataList = array();
        $data = $model->with('GoodsModel')->findAll($criteria);
        if (empty($data)) {
            $data = array();
        }
        foreach ($data as $rs) {
            $dataList[] = $rs->GoodsModel;
        }
        $this->assign("Page", $page->show());
        $this->assign('data', $dataList);
        $this->assign('count', $count);
        $this->render('/goods/taglist');
    }

}
